package com.wondersgroup.fjzlpt.console.dao.impl.role;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.wondersgroup.fjzlpt.console.dao.inter.role.RoleMenuActionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.Map;

/**
 * Created by lion on 2017/5/17.
 */
@Repository
public class RoleMenuActionDAOImpl extends BaseJTDAOImpl implements RoleMenuActionDAO {

    @Override
    @Autowired
    @Qualifier("TB_AUTH_ROLE_MENU_ACTION")
    public void setTb(TableBean tb) {
        super.setTb(tb);
    }

    /**
     * 查询角色对应的菜单功能
     * @param roleId
     * @return
     */
    public JSONArray queryByRoleId(String roleId)
    {
        StringBuilder sbud = new StringBuilder("select * from ")
                .append(this.tb.getTableName()).append(" where roleid=?");
        SQLBean sb = new SQLBean(sbud.toString(), new Object[]{roleId});
        return this.queryList(sb);
    }

    @Override
    public void inserBatch(String roleId, JSONArray ary) {
        if(ary.size() != 0)
        {
            for(Object obj:ary)
            {
                JSONObject json = (JSONObject)obj;
                json.put("ROLEID", roleId);
            }
            this.insertBatch(ary.toJavaList(Map.class));
        }
    }

    /**
     * 根据角色ID删除
     * @param roleId
     */
    public void deleteByRoleId(String roleId)
    {
        StringBuilder sbud = new StringBuilder("delete from ")
                .append(this.tb.getTableName()).append(" where roleid=?");
        SQLBean sb = new SQLBean(sbud.toString(), new Object[]{roleId});
        this.update(sb);
    }

    /**
     * 根据菜单删除
     * @param menuId
     */
    public void deleteByMenuId(String menuId)
    {
        StringBuilder sbud = new StringBuilder("delete from ")
                .append(this.tb.getTableName()).append(" where menuId=?");
        SQLBean sb = new SQLBean(sbud.toString(), new Object[]{menuId});
        this.update(sb);
    }
}
